
var Login = function() {
  this.init()
}

Login.prototype.init = function() {
  this.getElements().bindEvents(this)
}

Login.prototype.getElements = function() {
  this.$body = $(document.body)
  this.$LoginForm = this.$body.find('.login-form')
  this.$loginCheckbox = this.$LoginForm.find('.login-checkbox')
  this.$loginCheckbox.$checkbox = this.$id('checkbox')
  this.$loginTips = this.$LoginForm.find('.login-tips')
  return this
}

Login.prototype.bindEvents = function(login) {
  this.$body.on('click', '#checkbox', function() {
    if (this.checked) {
      login.$loginCheckbox.addClass('checked')
    } else {
      login.$loginCheckbox.removeClass('checked')
    }
  })
  this.$LoginForm.on('submit', function(e) {
    e.preventDefault()
    var username = login.$id('username')
    var password = login.$id('password')
    if (!$.trim(username.value)) {
      return login.alertTip('请输入您的工号~')
    }
    if (!$.trim(password.value)) {
      return login.alertTip('请输入密码~')
    }
    var checked = login.$loginCheckbox.$checkbox.checked

    login.handleSubmit(username, password, checked)
  })
}

Login.prototype.handleSubmit = function(username, password, checked) {
  this.alertTip('温馨提示：您的用户名或者密码不正确')
}

Login.prototype.alertTip = function(msg) {
  if (!msg) return
  var login = this
  this.$loginTips.html(msg).show()
  clearTimeout(this.tipTimer)
  this.tipTimer = setTimeout(function() {
    login.$loginTips.fadeOut()
  }, 1500)
}

Login.prototype.$id = function(id) {
  return document.getElementById(id)
}

module.exports = Login
